import os
import struct

#data = 0x5555

#f = open('./a.txt','rb')

#size = os.path.getsize('./a.txt')
#for i in range(size):
#    data += int.from_bytes(f.read(1), byteorder='big', signed=False)
#         #如何获取data数据类型
#print("校验和：\n")
#print('%#x'%data)

#f.close()

with open('C:/Users/hi_li/Desktop/python/test.txt', 'r') as file_input:
    file_intput_str = file_input.read()
    print('open for read...')
    print(file_intput_str)

input_str = ''
output_val = 0
finish_flag = 1
output_list = []
for i in range(len(file_intput_str)):
    if((file_intput_str[i] >= '0') and (file_intput_str[i] <= '9')):
        input_str += file_intput_str[i]
        finish_flag = 0
    elif((file_intput_str[i] >= 'a') and (file_intput_str[i] <= 'f')):
        input_str += file_intput_str[i]
        finish_flag = 0
    elif((file_intput_str[i] >= 'A') and (file_intput_str[i] <= 'F')):
        input_str += file_intput_str[i]
        finish_flag = 0
    else:
        b = int(input_str,16) #'a' --> 10
        output_list.append(b)
        input_str = ''
        finish_flag = 1
if(finish_flag == 0):
        b = int(input_str,16) #'a' --> 10
        output_list.append(b)
        input_str = ''
        finish_flag = 1    


print(output_list)
with open('C:/Users/hi_li/Desktop/python/test.bin', 'wb+') as file_output:
    #for i in range(len(output_list)):
    #    file_output.write(output_list[i])
    for x in output_list:
        a = struct.pack('B', x)
        file_output.write(a)
        print(a)
print('txt2bin successed ...')

